﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;

namespace WXYW
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserService userService = new UserService();
                roleTypeDropdownList.DataSource = userService.GetUserTypeList();
                roleTypeDropdownList.DataTextField = "Name";
                roleTypeDropdownList.DataValueField = "ID";
                roleTypeDropdownList.DataBind();

                ServiceStationService stationService = new ServiceStationService();
                StationDDL.DataSource = stationService.GetAllWithSelectOne();
                StationDDL.DataTextField = "Name";
                StationDDL.DataValueField = "ID";
                StationDDL.DataBind();

                ProductClassService classService = new ProductClassService();
                OEMProductClassDDL.DataSource = classService.GetOEMProdutClassWithSelectOne();
                OEMProductClassDDL.DataTextField = "Name";
                OEMProductClassDDL.DataValueField = "ID";
                OEMProductClassDDL.DataBind();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                string customerID = customerIDTxtBox.Text;
                string contact = contactTxtBox.Text;
                string phone = phoneTxtBox.Text;
                string mobilePhone = mobilePhoneTxtBox.Text;
                string password = passwordTxtBox.Text;
                string fax = faxTxtBox.Text;
                string companyName = companyNameTxtBox.Text;
                int roleType = Convert.ToInt32(roleTypeDropdownList.SelectedValue);
                string email = emailTxtBox.Text;
                string address = addressTxtBox.Text;
                string fullName = customerNameTxtBox.Text;
                string comment = commentTxtBox.Text;

                UserService userService = new UserService();
                userService.AddUser(customerID, contact, phone, mobilePhone, fax, companyName, password, roleType, email,
                    fullName, address, companyName, ContextService.CurrentAccount.UserID, Convert.ToInt32(StationDDL.SelectedValue),
                    Convert.ToInt32(OEMProductClassDDL.SelectedValue));
                MsgLbl.Text = "用户添加成功，请继续添加下一个用户。";

                ClearUserForm();
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        private void ClearUserForm()
        {
            customerIDTxtBox.Text = "";
            contactTxtBox.Text = "";
            phoneTxtBox.Text = "";
            mobilePhoneTxtBox.Text = "";
            passwordTxtBox.Text = "";
            faxTxtBox.Text = "";
            companyNameTxtBox.Text = "";
            emailTxtBox.Text = "";
            addressTxtBox.Text = "";
            customerNameTxtBox.Text = "";
            commentTxtBox.Text = "";
        }
    }
}
